package com.miui.cordova;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
//import android.text.TextUtils;
import android.util.Log;
//import android.widget.Toast;

import com.xiaomi.mipush.sdk.ErrorCode;
import com.xiaomi.mipush.sdk.MiPushClient;
import com.xiaomi.mipush.sdk.MiPushCommandMessage;
import com.xiaomi.mipush.sdk.MiPushMessage;
import com.xiaomi.mipush.sdk.PushMessageReceiver;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 1、PushMessageReceiver是个抽象类，该类继承了BroadcastReceiver。
 * 2、需要将自定义的DemoMessageReceiver注册在AndroidManifest.xml文件中 <receiver
 * android:exported="true"
 * android:name="com.xiaomi.mipushdemo.DemoMessageReceiver"> <intent-filter>
 * <action android:name="com.xiaomi.mipush.RECEIVE_MESSAGE" /> </intent-filter>
 * <intent-filter> <action android:name="com.xiaomi.mipush.ERROR" />
 * </intent-filter> </receiver>
 * 3、DemoMessageReceiver的onCommandResult方法用来接收客户端向服务器发送命令后的响应结果
 * 4、DemoMessageReceiver的onReceiveMessage方法用来接收服务器向客户端发送的消息
 * 5、onReceiveMessage和onCommandResult方法运行在非UI线程中
 *
 * @author wangkuiwei
 */
public class MessageReceiver extends PushMessageReceiver {

    @Override
    public void onReceiveMessage(Context context, MiPushMessage message) {
        Log.v(MiServicePlugin.TAG,
                "onReceiveMessage is called. " + message.toString());
        //String msg = String.format("Receive a message. Content is \"%1$s\"", message.getContent());
        try {
            JSONObject msg = new JSONObject();
            msg.put("messageId", message.getMessageId());
            msg.put("messageType", message.getMessageType());
            msg.put("content", message.getContent());
            msg.put("alias", message.getAlias());
            msg.put("topic", message.getTopic());
            msg.put("notifyType", message.getNotifyType());
            msg.put("notifyId", message.getNotifyId());
            msg.put("notified", message.isNotified());
            msg.put("description", message.getDescription());
            msg.put("title", message.getTitle());
            msg.put("category", message.getCategory());
            msg.put("passThrough", message.getPassThrough());
            msg.put("extra", message.getExtra());
            MiServicePlugin.transmitPush(msg.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onCommandResult(Context context, MiPushCommandMessage message) {
        Log.v(MiServicePlugin.TAG,
                "onCommandResult is called. " + message.toString());
        try {
            JSONObject msg = new JSONObject();
            msg.put("command", message.getCommand());
            msg.put("commandArguments", message.getCommandArguments());
            msg.put("resultCode", message.getResultCode());
            msg.put("reason", message.getReason());
            msg.put("category", message.getCategory());

            MiServicePlugin.transmitResult(msg.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return;
        /*
        String command = message.getCommand();
        List<String> arguments = message.getCommandArguments();
        String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments.get(0) : null);
        String cmdArg2 = ((arguments != null && arguments.size() > 1) ? arguments.get(1) : null);
        String log = "";
        if (MiPushClient.COMMAND_REGISTER.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                log = "Register push success.";
            } else {
                log = "Register push fail.";
            }
        } else if (MiPushClient.COMMAND_SET_ALIAS.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                log = String.format("Set alias \"%1$s\" success.", cmdArg1);
            } else {
                log = String.format("Set alias fail for %1$s.", message.getReason());
            }
        } else if (MiPushClient.COMMAND_UNSET_ALIAS.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                log = String.format("Unset alias \"%1$s\" success.", cmdArg1);
            } else {
                log = String.format("Unset alias fail for %1$s.", message.getReason());
            }
        } else if (MiPushClient.COMMAND_SUBSCRIBE_TOPIC.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                log = String.format("Subscribe topic \"%1$s\" success.", cmdArg1);
            } else {
                log = String.format("Subscribe topic fail for %1$s.", message.getReason());
            }
        } else if (MiPushClient.COMMAND_UNSUBSCRIBE_TOPIC.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                log = String.format("Unsubscribe topic \"%1$s\" success.", cmdArg1);
            } else {
                log = String.format("Unsubscribe topic fail for %1$s.", message.getReason());
            }
        } else if (MiPushClient.COMMAND_SET_ACCEPT_TIME.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                log = String.format("Set accept time %1$s - %2$s success.", cmdArg1, cmdArg2);
            } else {
                log = String.format("Set accept time fail for %1$s.", message.getReason());
            }
        } else {
            log = message.getReason();
        }
        */
        //MiServicePlugin.transmitResult("[" + getSimpleDate() + "]" + log);
    }

    @SuppressLint("SimpleDateFormat")
    public static String getSimpleDate() {
        return new SimpleDateFormat("MM-dd hh:mm:ss").format(new Date());
    }

}
